import axios from "axios";
import store from  "../store/index"
import Message from "element-ui/packages/message/src/main";
import da from "element-ui/src/locale/lang/da";
import router from "@/router";
export default function request(config) {
    //创建axios实例
    const instance = axios.create({
        baseURL: '/api',
        timeout: 5000
    })
    //axios拦截
    //请求拦截
    instance.interceptors.request.use(config=>{
        //console.log(config)
        //判断是否存在token，如果存在将每个页面header都添加token
        // console.log("------------"+store.state.token+"------------")
        if(store.state.token){
            config.headers['token']=store.state.token
        }

        return config
    },error => {

    })

    //拦截结果
    instance.interceptors.response.use(res=>{

  //  localStorage.setItem("token", JSON.stringify(res.headers.token))
        if (res.config.url=="/login"){
            return res
        }

      //  console.log(res)
        if (res.data.code===2002){
          return router.replace('/login')
        }

        return res.data
    },error => {})
    //console.log(config)
    //发送真正的网咯请求
    return instance(config)
}